package com.jingdianjichi.subject.domain.handler.subject;


import com.jingdianjichi.subject.common.entity.SubjectAnswer;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectTypeEnum;
import com.jingdianjichi.subject.domain.convert.RadioSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/*
* 单选题
* */
@Component
@Slf4j
public class RadioTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
       //插入单选题目
        List<SubjectRadio> subjectRadioList = new LinkedList<>();
        subjectInfoBO.getOptionLists().forEach(option->{
            SubjectRadio subjectRadio = new SubjectRadio();
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UNDELETED.getCode());
            subjectRadio.setOptionType(option.getOptionType());
            subjectRadio.setIsCorrect(option.getIsCorrect());
            subjectRadio.setOptionContent(option.getOptionContent());
            subjectRadioList.add(subjectRadio);
        });
        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectTypeEnum getHandlerType() {
        return SubjectTypeEnum.RADIO;
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        List<SubjectRadio> subjectRadioList = subjectRadioService.queryBySubjectId(Long.valueOf(subjectId));
        log.info("SubjectOptionBO.query.subjectRadio:{}",subjectRadioList);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();

        List<SubjectAnswer> optionList = new ArrayList<>();
        subjectRadioList.forEach(subjectRadio -> {
            SubjectAnswer subjectAnswer = new SubjectAnswer();
            subjectAnswer.setIsCorrect(subjectRadio.getIsCorrect());
            subjectAnswer.setOptionType(subjectRadio.getOptionType());
            subjectAnswer.setOptionContent(subjectRadio.getOptionContent());
            optionList.add(subjectAnswer);
        });
        subjectOptionBO.setOptionLists(optionList);
        return subjectOptionBO;
    }
}
